package apof.util
{
	import apof.services.Services;
	import apof.dao.Dao;
	import flash.filesystem.File;
	import flash.filesystem.FileStream;
	import flash.filesystem.FileMode;
	
	public class TransactionWriter
	{	
		private var ar:Array;
		private var startDay:Date;
		private var endDay:Date;
		private var file:File;
		private var fs:FileStream;
		private var xml:XML = 
<TR BgColor="#D4D0C8">
 <TD ALIGN="left" WIDTH="50"><FONT STYLE="font-family: 宋体; font-size: 10pt; color: #000000">日期</FONT></TD>
 <TD ALIGN="left" WIDTH="50"><FONT STYLE="font-family: 宋体; font-size: 10pt; color: #000000">帐户</FONT></TD>
 <TD ALIGN="left" WIDTH="50"><FONT STYLE="font-family: 宋体; font-size: 10pt; color: #000000">类别</FONT></TD>
 <TD ALIGN="left" WIDTH="200"><FONT STYLE="font-family: 宋体; font-size: 10pt; color: #000000">内容</FONT></TD>
 <TD ALIGN="left" WIDTH="50"><FONT STYLE="font-family: 宋体; font-size: 10pt; color: #000000">金额</FONT></TD>
 <TD ALIGN="left" WIDTH="200"><FONT STYLE="font-family: 宋体; font-size: 10pt; color: #000000">备注</FONT></TD>
</TR>
		private var headHTML:String = "<HTML><HEAD><TITLE></TITLE></HEAD><BODY><TABLE BORDER=1 CELLSPACING=0>" + xml.toXMLString();
		private var tailHTML:String = "</TR></TABLE></BODY></HTML>";
				
		public function TransactionWriter(startDay:Date,endDay:Date,file:File){
			this.startDay = startDay;
			this.endDay = endDay;
			this.file = file;
			Services.init(write);
			
		}
		
		
		public function write():void{
			ar = Services.getInstance().getTransaction(startDay,endDay,null,null);

			fs = new FileStream;
			fs.open(file,FileMode.WRITE);
			
			fs.writeUTFBytes(headHTML);
			ar.forEach(writeTransaction);
			fs.writeUTFBytes(tailHTML);
			fs.close();
		}
		
		public function writeTransaction(element:*,index:Number,arr:Array):void{
			fs.writeUTFBytes("<TR>");
			writeLine(fs,element.dtime);
			writeLine(fs,element.accountName);
			writeLine(fs,element.category);			
			writeLine(fs,element.name);
			writeLine(fs,element.amount);
			writeLine(fs,element.description);
			fs.writeUTFBytes("</TR>");
		}
		
		public function writeLine(fs:FileStream,data:String):void{
			var prefix:String = " <TD ALIGN=\"left\"><FONT STYLE=\"font-family: 宋体; font-size: 9pt; color: #000000\"> ";
			fs.writeUTFBytes(prefix+data+"</FONT></TD>");
			
		}
	}
}

